package cn.jbit.controller;

import cn.jbit.domain.Message;
import cn.jbit.entity.Employee;
import cn.jbit.service.EmployeeService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.aspectj.lang.annotation.Pointcut;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 财龙
 * @Description:
 * @date 2020/9/25 15:01
 */
@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

/*查询员工列表*/
    @GetMapping("getEmployeeList")
    public String getEmployeeList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, Model model){
        //开始分页
        PageHelper.startPage(pageNum,5);
        String  employeeList=employeeService.getEmployeeList();
        List<Employee> Employee = JSON.parseArray(employeeList, Employee.class);
        //计算分页后的结果，并封装
      /*  PageInfo <Employee> pageInfo = new PageInfo<>(employeeList,5);*/
        PageInfo<Employee> pageInfo =new PageInfo<>(Employee,5);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }

    /*使用json返回员工列表信息给前端*/
    @ResponseBody
    @GetMapping("getEmployeeListByJson")
    public Message getEmployeeListByJson(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        //开始分页
        PageHelper.startPage(pageNum,5);
        String  Employee=employeeService.getEmployeeList();
        List<Employee> employeeList = JSON.parseArray(Employee, Employee.class);
        //计算分页后的结果，并封装
        /*  PageInfo <Employee> pageInfo = new PageInfo<>(employeeList,5);*/
        PageInfo<Employee> pageInfo =new PageInfo<>(employeeList,5);

        if (CollectionUtils.isEmpty(pageInfo.getList())){
            return Message.fail();
        }
        return Message.success().add("pageInfo",pageInfo);
    }

    /**
     * 新增员工信息
     * @param employee
     * @return
     */
    @ResponseBody
    @PostMapping("saveEmployee")
    public Message saveEmployee(Employee employee){
        Boolean b = employeeService.addEmployee(employee);
        if(b){
            return Message.success();
        }
        return  Message.fail();
    }

/*验证员工姓名是否唯一*/
    @ResponseBody
    @GetMapping("checkEmpName")
    public Message saveEmployee(@RequestParam("empName") String empName ){
        Boolean qa = employeeService.checkEmpName(empName);
        if(qa){
            return Message.success();
        }
        return  Message.fail();
    }


    /*根据ID查出要修改的员工信息*/
    @ResponseBody
    @GetMapping("getEmployeeById/{empId}")
    public Message getEmployeeById(@PathVariable("empId") Integer empId){
Employee employee=employeeService.getEmployeeById(empId);
return Message.success().add("employee",employee);
    }


/*修改*/
@ResponseBody
@PutMapping("updateEmployee/{empId}")
    public  Message updateEmployee(@Valid Employee employee){
employeeService.updateEmployee(employee);
return  Message.success();
        }
        @ResponseBody
        @DeleteMapping("delEmployeeById/{ids}")
        public Message delEmployeeById(@PathVariable("ids") Integer ids){
    employeeService.delEmployeeById(ids);
    return Message.success();
        }
     /*删除*/
    @ResponseBody
    @DeleteMapping("delEmployeeByIds/{empId}")
    public Message delEmployeeByIds(@PathVariable("empId") String ids){
        if(ids.contains("-")){//判断传递过来的ID是否是多个
            ArrayList<Integer> list = new ArrayList<>();
            String[] strd = ids.split("-");//根据-进行分割
            for (String str : strd) {//遍历数组，将数据放到集合中
                list.add(Integer.parseInt(str));
            }
            employeeService.delEmployeeByIds(list);
        }else{
            int id = Integer.parseInt(ids);
            employeeService.delEmployeeById(id);
        }

        return Message.success();
    }
}

